package sdk.webview.fmc.com.fmcsdk.activity;

import android.content.Intent;
import android.net.http.SslError;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;



import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.base.BaseActivity;
import sdk.webview.fmc.com.fmcsdk.base.BasePresenter;
import sdk.webview.fmc.com.fmcsdk.h5.JsInterface;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.UserHelper;
import sdk.webview.fmc.com.fmcsdk.util.Utility;
import sdk.webview.fmc.com.fmcsdk.util.XLog;

public class LinkWebActivity extends BaseActivity  {

        private WebView webView;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_link_web;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        webView = (WebView) findViewById(R.id.webview);
        WebSettings webSetting = webView.getSettings();
        webSetting.setJavaScriptEnabled(true);
        String appCachePath = getCacheDir().getAbsolutePath();
        webSetting.setAppCachePath(appCachePath);
        webSetting.setAppCacheEnabled(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(true);
        webSetting.setSavePassword(false);
        // webSetting.setLoadWithOverviewMode(true);
        // webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setGeolocationEnabled(true);
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
//        webSetting.setBlockNetworkImage(true);
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        webView.addJavascriptInterface(new JsInterface(this, null), "android");
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed(); // Ignore SSL certificate errors
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                // 这里可自定义页面打开url，否则会调用系统自带浏览器打开url
                if (webView != null){
                    webView.loadUrl(url);
                    XLog.i(url);
                    return true;
                }

                return false;
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
            {
                //返回false，意味着请求过程里，不管有多少次的跳转请求（即新的请求地址），均交给webView自己处理，这也是此方法的默认处理
                //返回true，说明你自己想根据url，做新的跳转，比如在判断url符合条件的情况下，我想让webView加载http://ask.csdn.net/questions/178242

                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                String token = "";
                if (url.contains("accessToken")){
                    String array[]  = url.split("accessToken=");
                    token = array[1].split("#")[0];
                    Intent intents = new Intent();
                    intents.putExtra("token", token);
                    setResult(9784, intents);
                    finish();
                }
                super.onPageFinished(view, url);

            }
        });
        String address = sharedPrefs.getString(Constant.BASE_ADDRESS_URL, "");
        webView.loadUrl(address+"/uc/login?authType=2&clientType=1&imei="+Utility.getIMEI(this)+"&brand="+Build.BRAND+"&os=android&osversion="+ Build.VERSION.RELEASE+"&version=1.0");
        XLog.i(webView.getUrl()+"~~~~~~~~~~~~");
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }



}
